package com.whsxt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.domain.Order;
import com.whsxt.domain.OrderSettlement;
import com.whsxt.service.OrderService;
import com.whsxt.service.OrderSettlementService;
import com.whsxt.vo.OrderConfirmResult;
import com.whsxt.vo.OrderParam;
import com.whsxt.vo.OrderStatusResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-25 15:07
 * @Description:
 */
@RestController
@RequestMapping
@Api(tags = "订单管理接口")
public class OrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderSettlementService orderSettlementService;


    @GetMapping("/p/myOrder/orderCount")
    @ApiOperation(value = "查询当前用户的订单数量")
    public ResponseEntity<OrderStatusResult> findOrderCount() {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderStatusResult result = orderService.getOrderCount(openId);
        return ResponseEntity.ok(result);
    }


    @GetMapping("/p/myOrder/myOrder")
    @ApiOperation(value = "分页查询当前用户的订单")
    public ResponseEntity<Page<Order>> myOrderPage(Page<Order> page, Order order) {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        order.setUserId(openId);
        Page<Order> orderPage = orderService.findOrderPage(page, order);
        return ResponseEntity.ok(orderPage);
    }


    @PostMapping("/p/order/confirm")
    @ApiOperation(value = "订单确认接口")
    public ResponseEntity<OrderConfirmResult> confirmOrder(@RequestBody OrderParam orderParam) {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderConfirmResult orderPage = orderService.confirmOrder(orderParam, openId);
        return ResponseEntity.ok(orderPage);
    }


    @PostMapping("/p/order/submit")
    @ApiOperation(value = "订单提交")
    public ResponseEntity<String> orderSubmit(@RequestBody OrderConfirmResult orderConfirmResult) {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        String orderNum = orderService.orderSubmit(orderConfirmResult, openId);
        return ResponseEntity.ok("orderNum:" + orderNum);
    }


    @PostMapping("/getOrderByOrderNum")
    @ApiOperation(value = "根据订单号查询订单信息")
    public Order getOrderByOrderNum(@RequestParam String orderNum) {
        return orderService.getOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderNum)
        );
    }


    @PostMapping("/changeStatusOrderNum")
    @ApiOperation(value = "修改订单状态为已支付")
    void changeStatusByOrderNum(@RequestParam String orderNum) {
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderNum));
        if (order != null) {
            order.setStatus(2);
            order.setIsPayed(1);
            order.setPayTime(new Date());
            order.setUpdateTime(new Date());
            orderService.updateById(order);

            //更新订单结算表
            OrderSettlement orderSettlement = orderSettlementService.getOne(new LambdaQueryWrapper<OrderSettlement>()
                    .eq(OrderSettlement::getOrderNumber, orderNum));
            if (orderSettlement != null) {
                orderSettlement.setClearingTime(new Date());
                orderSettlement.setPayStatus(1);
                orderSettlement.setIsClearing(1);
                orderSettlementService.updateById(orderSettlement);
            }
        }
    }


    @GetMapping("/p/order/query")
    @ApiOperation(value = "订单支付状态查询")
    public ResponseEntity<String> getOrderStatus(@RequestParam String orderSn) {
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderSn)
                .eq(Order::getStatus, 1)
        );
        return ResponseEntity.ok(order != null ? "no" : "ok");
    }





}
